package com.buding.fx.view;

import com.buding.fx.common.constant.CodeFormatMainSize;
import com.buding.fx.service.impl.TableViewCreateService;
import com.jfoenix.controls.JFXButton;
import com.jfoenix.controls.JFXCheckBox;
import com.jfoenix.controls.JFXRadioButton;
import com.jfoenix.controls.JFXTreeTableView;
import com.jfoenix.svg.SVGGlyph;
import com.jfoenix.svg.SVGGlyphLoader;
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.ToggleGroup;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import lombok.SneakyThrows;


import java.awt.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Comparator;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @Author: mingdongwei
 * @Date: 2024-03-31
 * @Description:
 * @Version: 1.0.0
 */
public class CodeFormatView extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {
        SVGGlyphLoader.loadGlyphsFont(CodeFormatView.class.getResourceAsStream("/fonts/icomoon.svg"),
                "icomoon.svg");
        GridPane gridPane = new GridPane();
        gridPane.setStyle("-fx-background-color:WHITE");
        gridPane.setPrefWidth(CodeFormatMainSize.getInstance().getWIGTH());
        gridPane.setPrefHeight(CodeFormatMainSize.getInstance().getHEIGHT());

        VBox leftBoxType = new VBox();
        VBox rightBoxType = new VBox();

        createLeft(leftBoxType);
        createRight(rightBoxType);

        gridPane.add(leftBoxType, 0, 0);
        gridPane.add(rightBoxType, 1, 0);
        GridPane.setMargin(rightBoxType,new Insets(20,0,0,20));
        GridPane.setMargin(leftBoxType,new Insets(10,0,0,20));


        VBox leftBoxData = new VBox();
        VBox rightBoxData = new VBox();
        createLeftData(leftBoxData);
        createRightData(rightBoxData);
        gridPane.add(leftBoxData, 0, 1);
        gridPane.add(rightBoxData, 1, 1);
        GridPane.setMargin(leftBoxData,new Insets(20,0,0,20));
        GridPane.setMargin(rightBoxData,new Insets(10,0,0,20));


        Scene scene = new Scene(gridPane);

        primaryStage.setScene(scene);
        primaryStage.setTitle("文件代码转换器v0.24.04");
        primaryStage.centerOnScreen();
        primaryStage.setResizable(false);
        scene.getStylesheets().add("css/jfoenix-components.css");
        scene.getStylesheets().add("css/jfoenix-main-demo.css");

        primaryStage.show();
    }



    private void  createLeft(VBox leftBox){
        JFXButton button = new JFXButton("生成文件方式");
        button.setButtonType(JFXButton.ButtonType.RAISED);
        button.getStyleClass().add("custom-jfx-button-raised");
        leftBox.getChildren().add(button);
    }

    private void createRight(VBox rightBox){
        FlowPane flowPane = new FlowPane();
        flowPane.setHgap(20);
        JFXRadioButton jfxRadioButton1 = new JFXRadioButton("手动配置");

        jfxRadioButton1.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @SneakyThrows
            @Override
            public void handle(MouseEvent event) {
                int size = rightBox.getChildren().size();
                Node node = TableViewCreateService.create();
                if (size>1){
                    rightBox.getChildren().remove(rightBox.getChildren().get(size-1));
                }
                rightBox.getChildren().add(node);
                VBox.setMargin(node,new Insets(10,0,0,5));
            }
        });

        JFXRadioButton jfxRadioButton2 = new JFXRadioButton("文件导入");
        ToggleGroup group = new ToggleGroup();
        jfxRadioButton1.setToggleGroup(group);
        jfxRadioButton2.setToggleGroup(group);
        flowPane.getChildren().addAll(jfxRadioButton1,jfxRadioButton2);
        rightBox.getChildren().add(flowPane);
    }


    private void  createLeftData(VBox leftBox){
        JFXButton button = new JFXButton("表头调整");
        button.setButtonType(JFXButton.ButtonType.RAISED);
        button.getStyleClass().add("custom-jfx-button-raised");
        leftBox.getChildren().add(button);
    }

    private void createRightData(VBox rightBox) throws Exception {


        FlowPane flowPane = new FlowPane();
        SVGGlyph plus = SVGGlyphLoader.getIcoMoonGlyph("icomoon.svg.plus");
        plus.setSize(5);
        plus.setFill(Color.web("#4d804d"));
        JFXButton addButton = new JFXButton("新增",plus);

        JFXButton editButton = new JFXButton("修改");
        JFXButton delete = new JFXButton("删除");
        flowPane.getChildren().addAll(addButton,editButton,delete);

        JFXTreeTableView jfxTreeTableView = new JFXTreeTableView();
        jfxTreeTableView.setPrefHeight(100);

        rightBox.getChildren().addAll(flowPane,jfxTreeTableView);
    }

}